package app.view.component 
{
	import app.facade.App;
	import app.view.component.movie.Control;
	import app.view.component.movie.ItemContainer;
	import app.view.component.movie.MayContainer;
	import app.view.component.movie.McResult;
	import app.view.component.movie.McStart;
	import app.view.component.movie.Setting;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.temple.core.CoreMovieClip;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author hungconcon
	 */
	[Embed(source="../../../../lib/ui.swf", symbol="Game")]
	public class Game extends CoreMovieClip {
		
		//public var txtScore:TextField;
		public var mcScore:MovieClip;
		public var timer:Timer;
		
		public var mcControl:Control;
		public var mcStart:McStart;
		public var mcVachVoi:MovieClip;
		public var mcResult:McResult;
		public var mcSetting:Setting;
		public var mayCtn:MayContainer;
		public var itemCtn:ItemContainer;
		public var handSlider:MovieClip;
		public var face:FaceCore;
		
		public function Game(){
			addEventListener(Event.ADDED_TO_STAGE, onInital);
		}
		public function onInital(evt:Event):void {
			stage.addEventListener( Event.RESIZE, onResize );	
			setupMayCtn();
			setupItemCtn();
			setupMcResult();
			setupMcStart();
			setupControl();
			setupSetting();
			mcVachVoi.stop();
			onResize(evt);
			stopTimer();
			this.setChildIndex(handSlider, this.numChildren - 2 );
			this.setChildIndex(mcScore, this.numChildren -1);
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			if (App.load().data["mode"] != null) {
				App.MODE = App.load().data["mode"];	
			}
			if (App.MODE == 1) {
				handSlider.visible = false;
			}else {
				handSlider.visible = true;
			}
			
			mcScore.visible = false;
			face = new FaceCore();
		}
		
		public function	setupMcResult():void {
			mcResult = new McResult();
			mcResult.x = stage.stageWidth/2;
			mcResult.y = stage.stageHeight/2;
			mcResult.hide();
			this.addChild(mcResult);
		}
		
		public function	setupMcStart():void {
			mcStart = new McStart();
			mcStart.x = stage.stageWidth/2;
			mcStart.y = stage.stageHeight - 80;
			this.addChild(mcStart);
		}
		public function	setupControl():void {
			mcControl = new Control();
			mcControl.x = stage.stageWidth - 100;
			mcControl.y = 50;
			this.addChild(mcControl);
		}
		
		public function	setupSetting():void {
			mcSetting = new Setting();
			mcSetting.hide();
			mcSetting.x = stage.stageWidth/2;
			mcSetting.y = stage.stageHeight/2;
			this.addChild(mcSetting);
		}
		
		public function	setupMayCtn():void {
			mayCtn = new MayContainer();
			mayCtn.x = 0;
			mayCtn.y = 100;
			this.addChild(mayCtn);
		}
		
		public function	setupItemCtn():void {
			itemCtn = new ItemContainer();
			itemCtn.x = 0;
			itemCtn.y = 0;
			this.addChild(itemCtn);
		}
		
		
		public function onStartGame():void {
			App.isPlay = true;
			mcResult.hide();
			itemCtn.resume();
			mcScore.txtScore.htmlText = "0";
			startScore();
			mcVachVoi.play();
			mcScore.visible = true;
		}
		
		private function startScore():void 
		{
			if (timer == null) {
				timer = new Timer(1500);
				timer.addEventListener(TimerEvent.TIMER, onHanlerTimer);
			}
			timer.start();
		}
		
		private function stopTimer():void 
		{
			if (timer != null) {
				timer.stop();
			}
			
		}
		
		private function onHanlerTimer(evt:TimerEvent):void {	
			var score:int = int(mcScore.txtScore.text);
			mcScore.txtScore.htmlText = (score + 1).toString();
		}
		
		public function finishGame():void 
		{
			App.isPlay = false;
			stopTimer();
			mcResult.show();
			mcStart.hide();
			itemCtn.pause();
			var maxScore:Number = 0;
			if (App.load().data["score"] != null) {
				maxScore = App.load().data["score"];	
			}
			if (maxScore < Number(mcScore.txtScore.text)) {
				App.save("score", Number(mcScore.txtScore.text) );
				maxScore =  Number(mcScore.txtScore.text);
			}
			
			mcResult.setMaxScore(maxScore);
			mcVachVoi.stop();	
		}
		
		private function onStopFrameRePlay():void 
		{
			//mcReplay.stop();
		}
		
		private function onResize(evt:Event):void {
			//this.x = Math.round(stage.stageWidth / 2);
			//this.y = Math.round(stage.stageHeight / 2);
		}
		
		public function showSetting():void 
		{
			if (mcResult.visible == true) {
				mcResult.alpha = 0;
			}
			mcSetting.show();
		}
		
		public function hideSetting():void 
		{
			if (mcResult.visible == true) {
				mcResult.alpha = 1;
			}
			mcSetting.hide();
		}

		
		public function onhanlerSliderEvent(evt:MouseEvent):void 
		{
			switch (evt.type) 
			{
				case MouseEvent.MOUSE_DOWN:
					handSlider.arc.startDrag();
				break;
				case MouseEvent.MOUSE_UP:
					handSlider.arc.stopDrag();
					handSlider.arc.x = 0;
					handSlider.arc.y = 0;
				break;
				case MouseEvent.MOUSE_OUT:
					handSlider.arc.stopDrag();
					handSlider.arc.x = 0;
					handSlider.arc.y = 0;
				break;
				case MouseEvent.MOUSE_MOVE:
					calcMoveHand();
				break;
				default:
			}
		}
		
		private function calcMoveHand():void 
		{
			var arc:MovieClip = handSlider.arc;
			var bar:MovieClip = handSlider.bar;
			arc.y = 0;
			if (arc.x >= bar.width / 2) {
				arc.x = bar.width / 2;
			}
			
			if (arc.x <= -bar.width / 2) {
				arc.x = -bar.width / 2;
			}
		}
		
		public function onEnterFrame(evt:Event):void {
			if (App.isPlay == false) return;
			handSlider.arc.x = stage.mouseX - stage.stageWidth/2;
			if (handSlider.arc.x < -handSlider.bar.width/2) {
				handSlider.arc.x = -handSlider.bar.width / 2;
			}
			if (handSlider.arc.x > handSlider.bar.width/2) {
				handSlider.arc.x = handSlider.bar.width / 2;
			}
		}
		
		public function onMouseUp(evt:MouseEvent):void 
		{
			handSlider.arc.x = 0;
		}
		
		public function changeMode():void 
		{
			if (App.MODE == 1) {
				handSlider.visible = false;
			}else {
				handSlider.visible = true;
			}
			
			App.save("mode", { mode:App.MODE } );
		}
		
		public function shareHightScore():void {
			face.stage = this.stage;
			face.initFacebook();
		}
	}
}